package com.ita.osc.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import com.ita.osc.bean.User;
import com.ita.osc.dao.UserDao;
import com.ita.osc.db.DbConnection;
import com.ita.osc.util.UUIDUtil;

public class UserDaoImpl implements UserDao {
	
//	private Map<String, User> userList = new HashMap<String, User>();
//	private User user = new User(UUIDUtil.randomUUID(), "12345678911", "freedy", "123456");

	@Override
	public int createUser(User user) {
		// TODO Auto-generated method stub
		int m = 0;
		String sql = "INSERT INTO cuser (cuid, tel, uname, password) VALUES (?, ?, ?, ?)";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DbConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, user.getUid());
			pst.setString(2, user.getTel());
			pst.setString(3, user.getUname());
			pst.setString(4, user.getPassword());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return m;
	}

	@Override
	public User findUserById(String cuid) {
		// TODO Auto-generated method stub
		User user=null;
		String sql = "SELECT * FROM cuser where cuid = ?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DbConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, cuid);
			rs = pst.executeQuery();
			while (rs.next()) {
				String uid = rs.getString("cuid");
				String tel_ = rs.getString("tel");
				String uname = rs.getString("uname");
				String password = rs.getString("password");
				user = new User(uid, tel_, uname, password);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

	@Override
	public User findUserByTel(String tel) {
		// TODO Auto-generated method stub
		User user=null;
		String sql = "SELECT * FROM cuser where tel = ? or uname = ?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DbConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, tel);
			pst.setString(2, tel);
			rs = pst.executeQuery();
			while (rs.next()) {
				String uid = rs.getString("cuid");
				String tel_ = rs.getString("tel");
				String uname = rs.getString("uname");
				String password = rs.getString("password");
				user = new User(uid, tel_, uname, password);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

	@Override
	public User checkUser(String tel, String password) {
		// TODO Auto-generated method stub
		User user = findUserByTel(tel);
		if(user != null){
			if(password.equals(user.getPassword())){
				return user;
			}else {			
				return null;
			}
		}else {
			return null;
		}
	}

}
